import sys
import json
from pymongo import MongoClient
import pymongo
from decimal import Decimal
city=sys.argv[1]
db_name=sys.argv[2]
client=MongoClient("101.200.122.53",3719)
db=client['admin']
db.authenticate('root', 'zhuge1116')
db=client[db_name]
borough_price_file="%s/borough_price" % city
def default(obj):
    if isinstance(obj, Decimal):
        return str(obj)
    raise TypeError
newitem={}
with open(borough_price_file, 'w') as f:
    print 'in with'
    #for item in db['borough_list_price'].find({'update':20160719}).sort("date",pymongo.ASCENDING):
    for item in db['rent_borough_price'].find().sort("date",pymongo.ASCENDING):
        print item['borough_id']
        newitem[item['borough_id']] =  item['unit_rent_price']
    print 'before write'
    f.write(json.dumps(newitem,default=default))
    f.write("\n")

